﻿/* ***********************************************
 * Author		:  kingthy
 * Email		:  kingthy@gmail.com
 * Description	:  DbAccessorBase
 *
 * ***********************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace KT.Framework.Database
{
    /// <summary>
    /// 数据访问者基类
    /// </summary>
    public abstract class DbAccessorBase
    {
        /// <summary>
        /// 根据数据库帮助对象实例化
        /// </summary>
        /// <param name="dbHelper"></param>
        protected DbAccessorBase(DbHelper dbHelper)
        {
            this.DbHelper = dbHelper;
        }
        /// <summary>
        /// 数据库帮助对象
        /// </summary>
        public DbHelper DbHelper { get; private set; }
        
        /// <summary>
        /// 添加搜索条件
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="fieldName">字段名称</param>
        /// <param name="value">字段值]</param>
        protected virtual void AddSearchCondition(DbConditionBuilder condition, string fieldName, TrueOrFalse value)
        {
            if (value != TrueOrFalse.Any)
            {
                condition.Add(string.Format("{0}={1}", this.DbHelper.QuoteIdentifier(fieldName), this.DbHelper.GetDbParameterName(fieldName)), this.DbHelper.CreateDbParameter(fieldName, DbType.Boolean, value == TrueOrFalse.True ? true : false));
            }
        }
        /// <summary>
        /// 添加搜索条件
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="fieldName">字段名称</param>
        /// <param name="dbType">字段数据类型</param>
        /// <param name="value">字段值, 如果值为类型的默认值则不写入条件</param>
        protected virtual void AddSearchCondition<T>(DbConditionBuilder condition, string fieldName, DbType dbType, T value)
            where T : struct
        {
            if (!value.Equals(default(T)))
            {
                condition.Add(string.Format("{0}={1}", this.DbHelper.QuoteIdentifier(fieldName), this.DbHelper.GetDbParameterName(fieldName)), this.DbHelper.CreateDbParameter(fieldName, dbType, value));
            }
        }
        /// <summary>
        /// 添加搜索条件
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="fieldName">字段名称</param>
        /// <param name="value">字段值, 如果值为默认值则不写入条件</param>
        /// <param name="equalWithAndOP">是否是用"与运算"来判断成立条件</param>
        protected virtual void AddSearchCondition<T>(DbConditionBuilder condition, string fieldName, T value, bool equalWithAndOP)
            where T : struct
        {
            if (!value.Equals(default(T)))
            {
                if (equalWithAndOP)
                {
                    condition.AddFormat("({0}&{1})={1})", this.DbHelper.QuoteIdentifier(fieldName), value);
                }
                else
                {
                    condition.AddFormat("({0}={1})", this.DbHelper.QuoteIdentifier(fieldName), value);
                }
            }
        }
        /// <summary>
        /// 添加搜索条件
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="fieldName">字段名称</param>
        /// <param name="dbType">字段数据类型</param>
        /// <param name="value">字段值, 如果值为0则不写入条件</param>
        protected virtual void AddSearchCondition<T>(DbConditionBuilder condition, string fieldName, DbType dbType, ValueRange<T> value)
            where T : struct
        {
            if (value != null && value.HasValue)
            {
                string fieldStart = string.Concat(fieldName, "_start");
                string fieldEnd = string.Concat(fieldName, "_end");
                if (value.Start.HasValue && value.End.HasValue)
                {
                    condition.Add(string.Format("({0}>={1} AND {0}<={2})", this.DbHelper.QuoteIdentifier(fieldName),
                        this.DbHelper.GetDbParameterName(fieldStart),
                        this.DbHelper.GetDbParameterName(fieldEnd)),
                        this.DbHelper.CreateDbParameter(fieldStart, dbType, value.Start.Value),
                        this.DbHelper.CreateDbParameter(fieldEnd, dbType, value.End.Value));
                }
                else if (value.Start.HasValue)
                {
                    condition.Add(string.Format("{0}>={1}", this.DbHelper.QuoteIdentifier(fieldName), this.DbHelper.GetDbParameterName(fieldStart)),
                        this.DbHelper.CreateDbParameter(fieldStart, dbType, value.Start.Value));
                }
                else
                {
                    condition.Add(string.Format("{0}<={1}", this.DbHelper.QuoteIdentifier(fieldName), this.DbHelper.GetDbParameterName(fieldEnd)),
                        this.DbHelper.CreateDbParameter(fieldEnd, dbType, value.End.Value));
                }
            }
        }
        /// <summary>
        /// 添加搜索条件
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="fieldName">字段名称</param>
        /// <param name="dbType">字段数据类型</param>
        /// <param name="value">字段值, 如果值为0则不写入条件</param>
        protected virtual void AddSearchCondition(DbConditionBuilder condition, string fieldName, DbType dbType, DateTimeRange value)
        {
            if (value != null && value.HasValue)
            {
                string fieldStart = string.Concat(fieldName, "_start");
                string fieldEnd = string.Concat(fieldName, "_end");
                if (value.Start.HasValue && value.End.HasValue)
                {
                    condition.Add(string.Format("({0}>={1} AND {0}<={2})", this.DbHelper.QuoteIdentifier(fieldName),
                        this.DbHelper.GetDbParameterName(fieldStart),
                        this.DbHelper.GetDbParameterName(fieldEnd)),
                        this.DbHelper.CreateDbParameter(fieldStart, dbType, value.Start.Value),
                        this.DbHelper.CreateDbParameter(fieldEnd, dbType, value.End.Value));
                }
                else if (value.Start.HasValue)
                {
                    condition.Add(string.Format("{0}>={1}", this.DbHelper.QuoteIdentifier(fieldName), this.DbHelper.GetDbParameterName(fieldStart)),
                        this.DbHelper.CreateDbParameter(fieldStart, dbType, value.Start.Value));
                }
                else
                {
                    condition.Add(string.Format("{0}<={1}", this.DbHelper.QuoteIdentifier(fieldName), this.DbHelper.GetDbParameterName(fieldEnd)),
                        this.DbHelper.CreateDbParameter(fieldEnd, dbType, value.End.Value));
                }
            }
        }
        /// <summary>
        /// 添加搜索条件
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="fieldName">字段名称</param>
        /// <param name="dbType">字段数据类型</param>
        /// <param name="size">字段大小</param>
        /// <param name="value">字段值, 如果为空则不写入条件</param>
        protected void AddSearchCondition(DbConditionBuilder condition, string fieldName, DbType dbType, int size, string value)
        {
            this.AddSearchCondition(condition, fieldName, dbType, size, value, false);
        }
        /// <summary>
        /// 写入搜索条件
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="fieldName"></param>
        /// <param name="dbType"></param>
        /// <param name="size"></param>
        /// <param name="field"></param>
        protected void AddSearchCondition(DbConditionBuilder condition, string fieldName, DbType dbType, int size, SearchCriteriaField field)
        {
            if (field != null)
                this.AddSearchCondition(condition, fieldName, dbType, size, field.Value, field.Fuzzy);
        }
        /// <summary>
        /// 写入搜索条件
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="fieldName">字段名称</param>
        /// <param name="dbType">字段数据类型</param>
        /// <param name="size">字段大小</param>
        /// <param name="value">字段值, 如果为空则不写入条件</param>
        /// <param name="isFuzzy">是否模糊的</param>
        protected virtual void AddSearchCondition(DbConditionBuilder condition, string fieldName, DbType dbType, int size, string value, bool isFuzzy)
        {
            if (!string.IsNullOrEmpty(value))
            {
                if (isFuzzy)
                {
                    condition.Add(string.Format("{0} LIKE {1}", this.DbHelper.QuoteIdentifier(fieldName), this.DbHelper.GetDbParameterName(fieldName)), this.DbHelper.CreateDbParameter(fieldName, dbType, size, string.Concat("%", value, "%")));
                }
                else
                {
                    condition.Add(string.Format("{0}={1}", this.DbHelper.QuoteIdentifier(fieldName), this.DbHelper.GetDbParameterName(fieldName)), this.DbHelper.CreateDbParameter(fieldName, dbType, size, value));
                }
            }
        }
    }
}
